<?php

namespace app\db\model;

use think\Db;
use think\Model;

class DbOperationRecord extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $dbName     = 'qt_db_operation_record';
    # --------------------------------- 查询类接口 START------------------------------------
    /**
     * 获取记录详情信息
     * @param int  $id 数据库ID
     * @author MSQ 2021-8-30
    **/
    public function getDetails($id = 0,$field = '*')
    {
        $where = [
            'id'     => $id,
            'status' => 1,
        ];
        if ($field && $field != '*' && count(explode(',', $field)) == 1) {
            // 只取一个字段
            $info = $this->where($where)->value($field);
        }else{
            $info = db($this->dbName)->field($field)->where($where)->find();
        }        
        return $info;
    }
    # ---------------------------------- 查询类接口 END -------------------------------------
    
    # --------------------------------- 验证类接口 START ------------------------------------
    # ---------------------------------- 验证类接口 END -------------------------------------

 
    # ------------------------------- 数据处理类接口 START ----------------------------------
    /**
     * 添加记录
     * @param array  $data
     * @author MSQ 2021-8-30
    **/
    public function addData($data = [])
    {
        if (!$data) return false;

        $this->setAttr('id',null)->isUpdate(false)->save($data);

        if (!isset($this->id)) return false;
        
        return $this->id;
    }
    # --------------------------------- 数据处理类接口 END ----------------------------------
}
